import java.util.*;


class Solution {
    /**
     *LCR 033. 字母异位词分组
     * https://leetcode.cn/problems/sfvd7V/description/
     */
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap();
        //1、先把所有的字母异位词分组
        for(String s : strs) {
            char[] tmp = s.toCharArray();
            Arrays.sort(tmp);
            String key = new String(tmp);

            if(!map.containsKey(key)) {
                map.put(key, new ArrayList<>());
            }
            map.get(key).add(s);
        }
        //2、提取结果
        return new ArrayList(map.values());
    }
    /**
     * 219. 存在重复元素 II
     * https://leetcode.cn/problems/contains-duplicate-ii/description/
     */
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap();
        for(int i = 0; i < nums.length; i++) {
            if(map.containsKey(nums[i])) {
                if(i - map.get(nums[i]) <= k) return true;
            }
            map.put(nums[i], i);
        }
        return false;
    }
}